home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / AdobeExamples / NX_StrAdj / StrokeView.m < prev    next >
Text File  |  1995-06-12  |  7KB  |  223 lines

  1.  
  2. /*
  3.  * (a)  (C) 1990 by Adobe Systems Incorporated. All rights reserved.
  4.  *
  5.  * (b)  If this Sample Code is distributed as part of the Display PostScript
  6.  *    System Software Development Kit from Adobe Systems Incorporated,
  7.  *    then this copy is designated as Development Software and its use is
  8.  *    subject to the terms of the License Agreement attached to such Kit.
  9.  *
  10.  * (c)  If this Sample Code is distributed independently, then the following
  11.  *    terms apply:
  12.  *
  13.  * (d)  This file may be freely copied and redistributed as long as:
  14.  *    1) Parts (a), (d), (e) and (f) continue to be included in the file,
  15.  *    2) If the file has been modified in any way, a notice of such
  16.  *      modification is conspicuously indicated.
  17.  *
  18.  * (e)  PostScript, Display PostScript, and Adobe are registered trademarks of
  19.  *    Adobe Systems Incorporated.
  20.  * 
  21.  * (f) THE INFORMATION BELOW IS FURNISHED AS IS, IS SUBJECT TO
  22.  *    CHANGE WITHOUT NOTICE, AND SHOULD NOT BE CONSTRUED
  23.  *    AS A COMMITMENT BY ADOBE SYSTEMS INCORPORATED.
  24.  *    ADOBE SYSTEMS INCORPORATED ASSUMES NO RESPONSIBILITY
  25.  *    OR LIABILITY FOR ANY ERRORS OR INACCURACIES, MAKES NO
  26.  *    WARRANTY OF ANY KIND (EXPRESS, IMPLIED OR STATUTORY)
  27.  *    WITH RESPECT TO THIS INFORMATION, AND EXPRESSLY
  28.  *    DISCLAIMS ANY AND ALL WARRANTIES OF MERCHANTABILITY, 
  29.  *    FITNESS FOR PARTICULAR PURPOSES AND NONINFRINGEMENT
  30.  *    OF THIRD PARTY RIGHTS.
  31.  */
  32.  
  33. /*
  34.  *    StrokeView.m
  35.  *
  36.  *    The purpose of the application is to show the difference when stroke adjustment is
  37.  *    turned on and turned off.  The default state on the NeXT computer is ON.  Horizontal,
  38.  *    verticle, diagonal  lines and arcs are drawn at a set linespacing.  The linewidth can be
  39.  *    varied from the interface.
  40.  *
  41.  *    This file contains the methods for the StrokeView class, a subclass of view.
  42.  *    The important item to note is simply the effect that stroke adjustment has.  Refer
  43.  *    to the documentation for specifics on how stroke adjustment works.
  44.  *
  45.  *    Version:    2.0
  46.  *    Author:    John-Henry Gross, Ken Fromm
  47.  *    History:
  48.  *            03-07-91        Added this comment.
  49.  */
  50.  
  51. #import "StrokeView.h"
  52. #import "StrokeViewWraps.h"
  53. #import <appkit/Button.h>
  54. #import <appkit/Control.h>
  55. #import <appkit/Matrix.h>
  56. #import <appkit/TextField.h>
  57. #import <dpsclient/wraps.h>
  58. #include <math.h>
  59.  
  60. @implementation StrokeView
  61.  
  62. /* Allocate a gstate and set clipping to NO.  The gstate is allocated because we do not
  63. *  want to reset the individual drawing variables when the context is switched back to this
  64. *  view.  Place the line [self setClipping:NO]; after allocating the gstate to see the effect
  65. *  clipping has.
  66. *
  67. *  The PSWDefs() call sends some definitions used in the wraps to the interpreter.  They
  68. *  will be available in the interpreter when the wraps call them. 
  69. */
  70. + newFrame:(NXRect *) frm
  71. {
  72.     self = [super newFrame:frm]; 
  73.     [self allocateGState]; 
  74.     
  75.     PSWDefs ();
  76.  
  77.     return self;
  78. }
  79.  
  80. /* Free the gstate upon quitting. */
  81. - free
  82. {
  83.        [self  freeGState];
  84.     
  85.        return [super free];
  86. }
  87.  
  88. - setTimeDisplay:anObject
  89. {
  90.     timeDisplay = anObject;
  91.     return self;
  92. }
  93.  
  94. - setFieldWidth:anObject
  95. {
  96.     fieldWidth = anObject;
  97.     return self;
  98. }
  99.  
  100. - setMatrixLineTypes:anObject
  101. {
  102.     matrixLineTypes = anObject;
  103.     return self;
  104. }
  105.  
  106. - setStrokeAdjustment:(BOOL)strokeAdjValue
  107. {
  108.     strokeAdjustment = strokeAdjValue;
  109.     return self;
  110. }
  111.  
  112. /********************************************************************************************
  113. *    These static procedures are called within drawSelf:  They simply draw lines in
  114. *    the rectangle passed in.   They have been separated out of drawSelf:: mainly 
  115. *    for cosmetic reasons. 
  116. ********************************************************************************************/
  117.  
  118. /*  Adds horizontal lines to the path within rect. */
  119. static void makeHorizLines(NXRect  *rect)
  120. {
  121.     float     ypos, linespacing;
  122.     
  123.     linespacing = rect->size.height/NUMLINESHORIZ;
  124.     for (ypos = rect->origin.y; ypos <= rect->origin.y + rect->size.height; ypos += linespacing)
  125.         PSWMakeLine(rect->origin.x, ypos, rect->origin.x + rect->size.width, ypos);
  126.  
  127.     PSstroke();
  128. }
  129.  
  130. /*  Adds verticle lines to the path within rect. */
  131. static void makeVertLines(NXRect  *rect)
  132. {
  133.     float     xpos, linespacing;
  134.     
  135.     linespacing = rect->size.width/NUMLINESVERT;
  136.     for (xpos = rect->origin.x; xpos <= rect->origin .x + rect->size.width; xpos += linespacing)
  137.         PSWMakeLine(xpos, rect->origin.y, xpos, rect->origin.y + rect->size.height);
  138.  
  139.     PSstroke();
  140. }
  141.  
  142. /*  Adds diagonal lines to the path within rect.  The rectangle has to be clipped because
  143. *  the length of the line exceeds the height and the width.  Clipping will show only the 
  144. *  portion of the lines that fall within the rectangle.
  145.  */
  146. static void makeDiagLines(NXRect  *rect)
  147. {
  148.     float        angle, length;
  149.     
  150.     length = (float) sqrt(rect->size.width * rect->size.width + rect->size.height * rect->size.height);
  151.  
  152.     PSgsave();
  153.         PStranslate(rect->origin.x, rect->origin.y);
  154.     
  155.         for (angle = 0; angle <= 90; angle += DIAGDEGS) 
  156.         {
  157.             PSWMakeLine(0, 0, length, 0);
  158.             PSrotate(DIAGDEGS);
  159.         }
  160.         PSstroke();
  161.     PSgrestore();
  162. }
  163.  
  164.  
  165. /*  Adds arcs within rect.  Clipping is necessary because height is greater than the width
  166. *  and so some arcs will travel outside the rectangle
  167. */
  168. static void makeArcs(NXRect  *rect)
  169. {
  170.     float     radius, maxradius, spacing;
  171.     
  172.     maxradius = (float) sqrt(rect->size.width * rect->size.width +
  173.                          rect->size.height * rect->size.height);
  174.     spacing = maxradius/NUMARCS;
  175.     for (radius = spacing; radius <= maxradius; radius += spacing)
  176.             PSWMakeArc(rect->origin.x, rect->origin.y, radius, 0, 90);
  177.  
  178.     PSstroke();
  179.  
  180. }
  181.  
  182. /* Actions performed:  erase the display times in the interface, obtain the state of each
  183. *  line type button, clear the view by drawing a new background, set the values of the drawing
  184. *  variables, draw stroke adjusted lines and then non-stroke adjusted lines (displaying the
  185. *  times as well), reset the stroke adjustment and then draw the borders around the view.
  186. */
  187. - drawSelf:(NXRect *)r :(int) count
  188. {
  189.     int    ElapsedTime;
  190.  
  191.     [timeDisplay setStringValue:""];
  192.  
  193.     PSsetgray(BACKGROUND);
  194.     PSrectfill(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
  195.  
  196.     PSsetstrokeadjust(YES);
  197.     PSsetlinewidth(BORDERWIDTH);
  198.     PSsetgray(BORDERCOLOR);
  199.     PSrectstroke(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
  200.     
  201.     PSsetgray(LINECOLOR);
  202.     PSsetlinewidth([fieldWidth floatValue]);
  203.     PSsetstrokeadjust(strokeAdjustment);
  204.     
  205.     PSWMarkTime(); NXPing ();    
  206.         if ([[matrixLineTypes cellAt:0 :0] state])
  207.             makeHorizLines(&bounds);        
  208.          if ([[matrixLineTypes cellAt:0 :1] state])
  209.             makeVertLines(&bounds);        
  210.          if ([[matrixLineTypes cellAt:1 :0] state])
  211.             makeDiagLines(&bounds);
  212.         if ([[matrixLineTypes cellAt:1 :1] state])
  213.             makeArcs(&bounds);
  214.     PSWReturnTime (&ElapsedTime);
  215.  
  216.     [timeDisplay setIntValue:ElapsedTime];
  217.                 
  218.     return self;
  219. }
  220.  
  221.  
  222. @end
  223.